/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PackageUpdater.Update;

import PackageUpdater.Config.UpdateList;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class TestJFrame extends javax.swing.JFrame implements IUpdateTaskProgressEvent {

    private ArrayList<UpdateList> list = new ArrayList<UpdateList>();
    /**
     * Creates new form TestJFrame
     */
    public TestJFrame() throws Exception {
        initComponents();
        JUpdateManager.manager.setUpdateDownloadEvent(this);
        JUpdateManager.manager.setConfigPath("/home/wcss/PrivateFiles/UsefulFiles/packagebuilder测试/test/test.cfg");
        JUpdateManager.manager.loadConfig();
        JUpdateManager.manager.clearDownloadBuffers();
    }

    public void printLogs(String msg)
    {
       this.txtContent.setText(this.txtContent.getText() + msg + "\n");
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtContent = new javax.swing.JTextArea();
        btnDownUpdateList = new javax.swing.JButton();
        btnDownloadFiles = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        txtContent.setColumns(20);
        txtContent.setFont(new java.awt.Font("文泉驿微米黑", 0, 12)); // NOI18N
        txtContent.setRows(5);
        jScrollPane1.setViewportView(txtContent);

        btnDownUpdateList.setFont(new java.awt.Font("文泉驿微米黑", 1, 12)); // NOI18N
        btnDownUpdateList.setText("下载更新列表");
        btnDownUpdateList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownUpdateListActionPerformed(evt);
            }
        });

        btnDownloadFiles.setFont(new java.awt.Font("文泉驿微米黑", 1, 12)); // NOI18N
        btnDownloadFiles.setText("下载更新文件");
        btnDownloadFiles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownloadFilesActionPerformed(evt);
            }
        });

        btnStop.setFont(new java.awt.Font("文泉驿微米黑", 1, 12)); // NOI18N
        btnStop.setText("停止");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnDownUpdateList)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDownloadFiles, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop)
                        .addGap(0, 369, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDownUpdateList)
                    .addComponent(btnDownloadFiles)
                    .addComponent(btnStop))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDownUpdateListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownUpdateListActionPerformed
        try {
            // TODO add your handling code here:
            JUpdateManager.manager.searchUpdate();
        } catch (Exception ex) {
            Logger.getLogger(TestJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDownUpdateListActionPerformed

    private void btnDownloadFilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadFilesActionPerformed
        // TODO add your handling code here:
        JUpdateManager.manager.clearTaskQueueData();
        try {        
            JUpdateManager.manager.downloadUpdateEntry(list.get(0));
        } catch (Exception ex) {
            Logger.getLogger(TestJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDownloadFilesActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        try {
            // TODO add your handling code here:
            JUpdateManager.manager.stopAllTask();
        } catch (Exception ex) {
            Logger.getLogger(TestJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnStopActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new TestJFrame().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(TestJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDownUpdateList;
    private javax.swing.JButton btnDownloadFiles;
    private javax.swing.JButton btnStop;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtContent;
    // End of variables declaration//GEN-END:variables

    @Override
    public void handleUpdateList(JUpdateManager sender, String name) {
        printLogs("finish");
        UpdateList[] team = JUpdateManager.manager.checkNeedUpdateEntrys();
        for(UpdateList ul : team)
        {
           list.add(ul);
        }        
        printLogs("need:" + list.size());
        
    }

    @Override
    public void handleUpdateError(JUpdateManager sender, String name, String error) 
    {
        printLogs(error);
    }

    @Override
    public void handleUpdateFiles(JUpdateManager sender, String name) {
        printLogs("下载完成");
    }

    @Override
    public void handleUpdateDownloading(JUpdateManager sender, String name, long current, long total) {
        if (name.contains("%"))
        {
           txtContent.setText("");
           printLogs(name + ":" + current + "," + total);
        }
    }
}
